/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.ref;

import java.lang.reflect.Field;

/**
 * <p>Project: app2022 - R5
 * <p>Powered by webrx On 2022-02-09 16:15:34
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class R5 {
    public static void main(String[] args) throws NoSuchFieldException {
        var u = User.class;

        //反射读取类上的注解
        if (u.isAnnotationPresent(UserName.class)) {
            var uc = u.getAnnotation(UserName.class);
            System.out.println(uc.name());
        }

        //读取属性上的注解
        Field f = u.getDeclaredField("name");
        if (f.isAnnotationPresent(UserName.class)) {
            var uc = f.getAnnotation(UserName.class);
            System.out.println(uc.name());
        }

        //读取方法上的注解
        User us = new User();
        us.show();
        try {
            var m = u.getDeclaredMethod("show");
            if (m.isAnnotationPresent(UserName.class)) {
                System.out.println(m.getAnnotation(UserName.class).name());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
